<%@page import="frontend.HttpStatus"%>
<%@page import="javax.script.ScriptEngine"%>
<%@page import="javax.script.ScriptEngineManager"%>
<%@page import="backend.exceptions.ElementNotFoundException"%>
<%@page import="backend.db.ORM"%>
<%@page import="backend.Session"%>
<%@page import="backend.Course"%>
<%
	String username = request.getUserPrincipal().getName();
	
	if (request.isUserInRole("student")) {
%>
<script type="text/javascript" src="student/myCourses.js"></script>
<script type="text/javascript" src="student/registerToCourses.js"></script>
<%} %>
<link rel="stylesheet" type="text/css" href="style/coursesTable.css" />
<div id="main-viewCourse" style="font-size:150%;">
	<%
		final String courseNameParam = request.getParameter("courseName");
      	 	final String courseName;
        if (null != courseNameParam) {
			ScriptEngineManager factory = new ScriptEngineManager();
	        ScriptEngine engine = factory.getEngineByName("JavaScript");
        	courseName = (String)engine.eval("decodeURIComponent('" + courseNameParam + "')");
        } else
        	courseName = null;
		Course course = null;
		try {	
			if (null != courseName)
				course = ORM.getCourseByName(courseName);
			else {
				String courseId = request.getParameter("courseId");
				if (null != courseId)
					course = ORM.getCourseById(courseId);
				else { // no argument was passed
					response.sendError(HttpStatus.SC_BAD_REQUEST);
					return;
				}
			}
		} catch (final ElementNotFoundException e) {
			course = null;
		}
		if (null == course) {
			//course doesn't exist
			out.print("<h2>We're sorry, but no such course exists.</h2>");
		} else {
	%>
	<fieldset>
	<legend style="font-size:75%;">Course information: </legend>
	<div>
		<table style="width:50%;">
			<tbody>
				<tr>
					<th style="width:40%;text-align:left;">Course name:</th>
					<td><%=course.courseName %></td>
				</tr>
				<tr>
					<th style="width:40%;text-align:left;">Course id:</th>
					<td><%=course.courseId %></td>
				</tr>
				<tr>
					<th style="width:40%;text-align:left;">Credit points:</th>
					<td><%=course.creditPoints %></td>
				</tr>
				<tr>
					<th style="width:40%;text-align:left;">Group number:</th>
					<td><%=course.groupNum %></td>
				</tr>
				<tr>
					<th style="width:40%;text-align:left;">Moed A:</th>
					<td><%=course.moedA %></td>
				</tr>
				<tr>
					<th style="width:40%;text-align:left;">Moed B:</th>
					<td><%=course.moedB %></td>
				</tr>
				<tr>
					<th style="width:40%;text-align:left;">Capacity:</th>
					<td><%=course.capacity %></td>
				</tr>
				<tr>
					<th style="width:40%;text-align:left;">Lecturer in charge:</th>
					<td><%=course.lecturerInCharge %></td>
				</tr>
				<tr>
					<th style="width:40%;text-align:left;">Description of the course:</th>
					<td style="text-align:left;vertical-align:text-top;font-size:75%;"><%=course.description %></td>
				</tr>
			</tbody>
		</table>
		<br />
	</div>
	<div>
		<%
			if (!course.lectures.iterator().hasNext())
				out.print("<h2>There are no lectures in this course</h2>");
			else {
		%>
		<h2>Lectures</h2>
		<table>
			<thead>
				<tr>
					<th style="width:32%;">Teacher</th>
					<th style="width:32%;">Location</th>
					<th style="width:16%;">Day</th>
					<th style="width:10%;">Start time</th>
					<th style="width:10%;">Duration</th>
				</tr>
			</thead>
			<tbody>
				<%
					for (Session s : course.lectures) {
				%>
				<tr>
					<td><%=s.teacherName %></td>
					<td><%=s.classLocation %></td>
					<td><%=s.day %></td>
					<td><%=s.startHour %>:00</td>
					<td><%=s.duration %></td>
				</tr>
				<%} %>
			</tbody>
		</table>
		<%} // has lectures%>
	</div>
	<div>
		<%
			if (!course.tutorials.iterator().hasNext())
				out.print("<h2>There are no tutorials in this course</h2>");
			else {
		%>
		<h2>Tutorials</h2>
		<table>
			<thead>
				<tr>
					<th style="width:32%;">Teacher</th>
					<th style="width:32%;">Location</th>
					<th style="width:16%;">Day</th>
					<th style="width:10%;">Start time</th>
					<th style="width:10%;">Duration</th>
				</tr>
			</thead>
			<tbody>
				<%
					for (Session s : course.tutorials) {
				%>
				<tr>
					<td><%=s.teacherName %></td>
					<td><%=s.classLocation %></td>
					<td><%=s.day %></td>
					<td><%=s.startHour %>:00</td>
					<td><%=s.duration %></td>
				</tr>
				<%} %>
			</tbody>
		</table>
		<%} // has tutorials%>
	</div>
	<%} // course exists%>
	</fieldset>
	<br></br>
	<div id="regUnregView" style="text-align:left;">
	<%
		if (request.isUserInRole("student")) {
				boolean isCourseAvailable = ORM.isCourseAvailable(username, course);
				if(isCourseAvailable == true)
				{
				%>
					<p id="<%=course.courseName%>" class="par2" onclick="registerFromView(this);">register</p>
				<%
				}
				else if(ORM.isUserRegisteredToCourse(username, course) == true)
				{
				%>
					<p id="<%=course.courseName%>" class="par" onclick="removeFromView(this);">unregister</p> 
				<%		
				}
	%>
	</div>
	<form id="registerForm" method="post" action="#">
		<div>
			<input type="hidden" name="username" value="<%=username %>" />
			<input type="hidden" name="course" value="" />
		</div>
	</form>	
	<form id="removeForm" method="post" action="#">
		<div>
			<input type="hidden" name="username" value="<%=username %>" />
			<input type="hidden" name="course" value="" />
		</div>
	</form>
	<script type="text/javascript">
		setPostFormToServlet('registerForm', 'student/AvailableCourses');
		setPostFormToServlet('removeForm', 'student/MyCourses');
	</script>
	<%} // isStudent
		else if (username.equals(course.owner)) {
	%>
	<form id="adminRemoveForm" method="post" action="#">
		<div>
			<input type="hidden" name="courseName" value="<%=course.courseName %>" />
			<input type="submit" value="Delete course" />
		</div>
	</form>
	<script type="text/javascript">
		setPostFormToServletWithCompleteFunc('adminRemoveForm', 'admin/removeCourse', function() {$('#allCoursesLink').click();});
	</script>
	<%} // isOwner %>
</div>
